package com.lg.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.common.pojo.ResponseResult;
import com.lg.product.entity.Spu;
import com.lg.product.entity.SpuDetail;
import com.lg.product.service.ISpecParamService;
import com.lg.product.service.ISpuDetailService;
import com.lg.product.service.ISpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * spu表，该表描述的是一个抽象性的商品，比如 iphone8 前端控制器
 * </p>
 *
 * @author lzk
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/spu")
public class SpuController {

    @Autowired
    ISpuService iSpuService;
    @Autowired
    ISpuDetailService iSpuDetailService;
    @Autowired
    ISpecParamService iSpecParamService;



    @RequestMapping("/list")
    public ResponseResult list(Page page, Spu spu){

        IPage iPage = iSpuService.selectSpuPage(page, spu);

        return ResponseResult.success(iPage);

    }

    @RequestMapping("/updateStatus")
    public ResponseResult updateStatus(@RequestBody Spu spu){

        return ResponseResult.success(iSpuService.updateById(spu));

    }


    @RequestMapping("/insert")
    public ResponseResult insert(@RequestBody Spu spu){

        Date date = new Date();
        spu.setCreateTime(date);
        spu.setLastUpdateTime(date);
        boolean save = iSpuService.saveOrUpdate(spu);
        System.out.println(spu);
        //添加spu_detail
        SpuDetail detail = new SpuDetail();
        detail.setSpuId(spu.getId());
        detail.setGenericSpec("");
        detail.setSpecialSpec("");
        detail.setPackingList(spu.getPackingList());
        detail.setAfterService(spu.getAfterService());
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("spu_id",spu.getId());
        List list = iSpuDetailService.list(wrapper);
        boolean save1;
        if(list.size()>0){
            System.out.println("修改");
            save1=iSpuDetailService.update(detail,wrapper);
        }else{
            System.out.println("添加");
            save1= iSpuDetailService.save(detail);
        }




        if(save&&save1){
            return ResponseResult.success(spu);
        }
        return ResponseResult.error("false");
    }



    @RequestMapping("/insertSpuDetail")
    public ResponseResult insertSpuDetail(@RequestBody SpuDetail spuDetail){
        System.out.println(spuDetail);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("spu_id",spuDetail.getSpuId());
        return ResponseResult.success( iSpuDetailService.update(spuDetail,wrapper));
    }


    @RequestMapping("/findBySpuId")
    public ResponseResult findBySpuId(Long spuId){

        Spu spu = iSpuService.getById(spuId);

        QueryWrapper wrapper = new QueryWrapper();

        wrapper.eq("spu_id",spuId);

        SpuDetail detail = iSpuDetailService.getOne(wrapper);

        spu.setPackingList(detail.getPackingList());

        spu.setAfterService(detail.getAfterService());

        System.out.println(spu);
        return ResponseResult.success(spu);
    }

    @RequestMapping("/selectSpecsByCid")
    public ResponseResult selectSpecsByCid(Long cid){
        System.out.println(cid);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cid",cid);
        wrapper.eq("generic",1);

        List list = iSpecParamService.list(wrapper);

        return ResponseResult.success(list);
    }



    @RequestMapping("/addGenericSpec")
    public ResponseResult addSpecs(String genericSpec, Long spuId){

        System.out.println(genericSpec);
        System.out.println(spuId);

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("spu_id",spuId);
        SpuDetail detail = new SpuDetail();
        detail.setGenericSpec(genericSpec);
        boolean  update=iSpuDetailService.update(detail,wrapper);

        return ResponseResult.success(update);
    }


    @RequestMapping("/addSpecialSpec")
    public  ResponseResult addSpecialSpec(@RequestBody SpuDetail spuDetail){
        System.out.println("specialSpec"+spuDetail);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("spu_id",spuDetail.getSpuId());
        boolean  update=iSpuDetailService.update(spuDetail,wrapper);
        return ResponseResult.success(true);
    }


    @RequestMapping("/initGenericSpec")
    public ResponseResult initGenericSpec(Long spuId){

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("spu_id",spuId);
        SpuDetail detail = iSpuDetailService.getOne(wrapper);
        String genericSpec = detail.getGenericSpec();

        if(genericSpec==null || genericSpec.equals("")){

            return ResponseResult.error("genericSpec为空");
        }

        return ResponseResult.success(genericSpec);
    }




    @RequestMapping("/del")
    public ResponseResult del(Long id){
        Spu spu = new Spu();
        spu.setId(id);
        spu.setValid(false);
        boolean b = iSpuService.updateById(spu);
        return ResponseResult.success(b);
    }



}
